package thread;

public class SyncDemo4 {
    public static void main(String[] args) {
        Boo b = new Boo();
        new Thread(b::methodA).start();
        new Thread(b::methodB).start();
    }
}

class Boo {
    public synchronized void methodA() {
        Thread t = Thread.currentThread();
        System.out.println(t.getName() + "正在执行A方法");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println(t.getName() + "执行A方法完毕");
    }
    public  void methodB() {
        synchronized (this){//同步块中的代码依然与A方法代码互斥
            Thread t = Thread.currentThread();
            System.out.println(t.getName() + "正在执行B方法");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(t.getName() + "执行B方法完毕");
        }

    }
}